
export default class EncryptUils {

    private static instance: EncryptUils;
    public static getInstance() {
        if (!this.instance) {
            this.instance = new EncryptUils();
        }
        return this.instance;
    }

    private EncryptUils(){
        
    }

    /**
     * sha1加密
     * @param data   
     * @param uppercase  加密结果字符串是否大写
     */
    public sha1(data: string, uppercase: boolean = false): Promise<string> {
        return new Promise((res, rej) => {
            import(/* webpackChunkName: "sha1" */'crypto-js/sha1').then((sha1) => {
                res(sha1.default(data).toString())
            });
        });
    }
    /**
     * 对称加密
     * @param data 
     * @param publicKey 公钥
     */
    public rsa(data: string, publicKey: string): Promise<string> {
        return new Promise((res, rej) => {
            import(/* webpackChunkName: "jsencrypt" */'jsencrypt').then((JSEncrypt) => {
                const jsEncrypt = new JSEncrypt.default();
                jsEncrypt.setPublicKey(publicKey);
                const value = jsEncrypt.encrypt(data);
                res(value);
            }).catch(rej);
        });

    }
}
